//
//  ViewController.m
//  SQLScrollview
//
//  Created by Mr - 宋 on 2022/7/30.
//

#import "ViewController.h"
#import "SQLScrollView.h"
#import "SQLSliderSwitchView.h"
#import "SQLOneViewController.h"
@interface ViewController ()<SQLSliderSwitchViewDeleagte,UIScrollViewDelegate>
#define TOPViewHeight 800
#define btnHeight  40
#define SQLWidth [UIScreen mainScreen].bounds.size.width
#define SQLHeight [UIScreen mainScreen].bounds.size.height
#define UIColorFromHex(s,a) [UIColor colorWithRed:(((s & 0xFF0000) >> 16))/255.0 green:(((s &0xFF00) >>8))/255.0 blue:((s &0xFF))/255.0 alpha:a]
#define SQL_WEAKSELF(weakSelf)  __weak __typeof(&*self)weakSelf = self;
@property (nonatomic,strong)SQLScrollView *scrollview;
@property (nonatomic,strong)NSMutableArray *oneArray,*twoArray;
@property (nonatomic,strong)UIButton *btnOne,*btnTwo,*selectBtn;
@property (nonatomic,strong)SQLSliderSwitchView *sliderSwitchView;
@property (nonatomic,assign)BOOL mainCanScroll;
@property (nonatomic,strong)SQLOneViewController *slectVC;
@end

@implementation ViewController
- (NSMutableArray *)oneArray{
    if (!_oneArray) {
        _oneArray = [NSMutableArray array];
        SQL_WEAKSELF(weakSelf);
        for (int i =0; i<6; i++) {
            SQLOneViewController *oneVC = [SQLOneViewController new];
            oneVC.tabTopBlock = ^{
                weakSelf.mainCanScroll = YES;
            };
            oneVC.title = [NSString stringWithFormat:@"第一个btn界面%d",i];
            [_oneArray addObject:oneVC];
        }
        return _oneArray;
    }
    return _oneArray;
}
- (NSMutableArray *)twoArray{
    if (!_twoArray) {
        _twoArray = [NSMutableArray array];
        SQL_WEAKSELF(weakSelf);
        for (int i =0; i<4; i++) {
            SQLOneViewController *oneVC = [SQLOneViewController new];
            oneVC.title = [NSString stringWithFormat:@"第二个btn界面%d",i];
            oneVC.tabTopBlock = ^{
                weakSelf.mainCanScroll = YES;
            };
            [_twoArray addObject:oneVC];
        }
    }
    return _twoArray;
}

- (UIButton *)setBtn{
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    btn.backgroundColor = [UIColor yellowColor];
    [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [btn addTarget:self action:@selector(btnSel:) forControlEvents:(UIControlEventTouchUpInside)];
    return btn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollview = [[SQLScrollView alloc]initWithFrame:CGRectMake(0, 0, SQLWidth, SQLHeight)];
    self.scrollview.delegate = self;
    self.scrollview.contentSize = CGSizeMake(SQLWidth, 2000);
    [self.view addSubview:self.scrollview];
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SQLWidth, TOPViewHeight)];
    topView.backgroundColor = [UIColor blueColor];
    [self.scrollview addSubview:topView];
    self.btnOne = [self setBtn];
    self.btnOne.tag = 101;
    self.btnOne.frame = CGRectMake(0, TOPViewHeight, SQLWidth/2, btnHeight);
    self.btnOne.backgroundColor = [UIColor redColor];
    [self.btnOne setTitle:@"第一个界面" forState:(UIControlStateNormal)];
    [self.scrollview addSubview:self.btnOne];
    self.selectBtn = self.btnOne;
    
    self.btnTwo = [self setBtn];
    self.btnTwo.tag = 102;
    self.btnTwo.frame = CGRectMake(SQLWidth/2, TOPViewHeight, SQLWidth/2, btnHeight);
    [self.btnTwo setTitle:@"第二个界面" forState:(UIControlStateNormal)];
    [self.scrollview addSubview:self.btnTwo];
    
    self.sliderSwitchView = [[SQLSliderSwitchView alloc]initWithFrame:CGRectMake(0, TOPViewHeight+btnHeight, SQLWidth, 800)];
    self.sliderSwitchView.delegate = self;
    _sliderSwitchView.backgroundColor = UIColorFromHex(0xF7F7F7, 1);
    [self.scrollview addSubview:self.sliderSwitchView];
    self.sliderSwitchView.tabItemTitleColor = UIColorFromHex(0x666666, 1);
    self.sliderSwitchView.BGColor = UIColorFromHex(0xF7F7F7, 1);
    self.sliderSwitchView.tabItemTitleFont = [UIFont systemFontOfSize:12];
    self.sliderSwitchView.tabItemTitleSelectedFont = [UIFont systemFontOfSize:15];
    self.sliderSwitchView.tabItemTitleSelectedColor = UIColorFromHex(0xFF7028, 1);
    self.sliderSwitchView.tabItemSelectedBgColor = UIColorFromHex(0xFF7028, 1);
    self.sliderSwitchView.viewControllers = self.oneArray;
    self.mainCanScroll = YES;
}

- (void)btnSel:(UIButton *)sender{
    if (self.selectBtn == sender) {
        return;
    }
    self.selectBtn = sender;
    if (sender.tag == 101) {
        self.sliderSwitchView.viewControllers = self.oneArray;
        self.btnOne.backgroundColor = [UIColor redColor];
        self.btnTwo.backgroundColor = [UIColor yellowColor];
    }else{
        self.btnTwo.backgroundColor = [UIColor redColor];
        self.btnOne.backgroundColor = [UIColor yellowColor];
        self.sliderSwitchView.viewControllers = self.twoArray;
    }
}

- (void)scrollViewDidScroll: (UIScrollView *) scrollView{
    CGFloat offset = scrollView.contentOffset.y;
    if (!self.mainCanScroll) {
        scrollView.contentOffset = CGPointMake(0, 700);
        self.slectVC.isCanScroll = YES;
    }else{
        if (offset>= 700) {
            scrollView.contentOffset = CGPointMake(0, 700);
            self.mainCanScroll = NO;
            self.slectVC.isCanScroll = YES;
        }
    }
}

#pragma mark SQLSliderSwitchViewDeleagte
- (void)slideSwitchView:(SQLSliderSwitchView *)view didselectTab:(NSUInteger)number{
    if (self.selectBtn.tag == 101) {
        self.slectVC = self.oneArray[number];
        [self.slectVC getData];
    }else{
        self.slectVC = self.twoArray[number];
        [self.slectVC getData];
    }
}


@end
